package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.DelaysayuListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QuarantineDetailListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QunCdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.TypeListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.ValidFlagListItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

//new
public class QuarantineResultDetailThread extends BasicThread {

	//new
	public interface QuarantineResultDetailThreadCallback {
		public void OnQuarantineDetailCompleted(boolean isNetworkConntected
				, QuarantineResultDetailItem quarantineItem, String message);
		public void OnQuarantineDetailError(ErrorItem errorItem);
	}

	private String url;
	private QuarantineResultDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String offCd;
	private String recNo;

	//new
	public QuarantineResultDetailThread(Context context
			, QuarantineResultDetailThreadCallback callback, String offCd
			, String recNo, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.recNo = recNo;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ010, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnQuarantineDetailCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnQuarantineDetailError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnQuarantineDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayQuarantine = json.getJSONArray(Const.DATA);
				JSONObject quarantine = arrayQuarantine.getJSONObject(0);
				
				QuarantineResultDetailItem quarantineItem = new QuarantineResultDetailItem();
				
				ArrayList<ValidFlagListItem> arrayVal = new ArrayList<ValidFlagListItem>();
				JSONArray valitems	 	= json.getJSONArray(QuarantineResultDetailItem.VALIDFLAGLIST);
				for(int i = 0; i < valitems.length(); i++) {
					JSONObject itemObject = valitems.getJSONObject(i);
					ValidFlagListItem valItem = new ValidFlagListItem();
					valItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ValidFlagListItem.CODE)));
					valItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ValidFlagListItem.CODENM)));
					arrayVal.add(valItem);
				}
				quarantineItem.setValidflagList(arrayVal);
				
				ArrayList<DelaysayuListItem> arrayDelay = new ArrayList<DelaysayuListItem>();
				JSONArray delayitems	 	= json.getJSONArray(QuarantineResultDetailItem.DELAYSAYULIST);
				for(int i = 0; i < delayitems.length(); i++) {
					JSONObject itemObject = delayitems.getJSONObject(i);
					DelaysayuListItem delayItem = new DelaysayuListItem();
					delayItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, DelaysayuListItem.CODE)));
					delayItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, DelaysayuListItem.CODENM)));
					arrayDelay.add(delayItem);
				}
				quarantineItem.setDelaysayuList(arrayDelay);
				
				ArrayList<QunCdItem> arrayQun = new ArrayList<QunCdItem>();
				JSONArray qunitems	 	= json.getJSONArray(QuarantineResultDetailItem.QUNCDLIST);
				for(int i = 0; i < qunitems.length(); i++) {
					JSONObject itemObject = qunitems.getJSONObject(i);
					QunCdItem qunItem = new QunCdItem();
					qunItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODE)));
					qunItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODENM)));
					arrayQun.add(qunItem);
				}
				quarantineItem.setQunCdList(arrayQun);
				
				ArrayList<TypeListItem> arrayType = new ArrayList<TypeListItem>();
				JSONArray typeitems	 	= json.getJSONArray(QuarantineResultDetailItem.TYPELIST);
				for(int i = 0; i < typeitems.length(); i++) {
					JSONObject itemObject = typeitems.getJSONObject(i);
					TypeListItem typeItem = new TypeListItem();
					typeItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, TypeListItem.CODE)));
					typeItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, TypeListItem.CODENM)));
					arrayType.add(typeItem);
				}
				quarantineItem.setTypeList(arrayType);
				
				ArrayList<QuarantineDetailListItem> arrQuarantine = new ArrayList<QuarantineDetailListItem>();
				JSONArray quaItems = json.getJSONArray(QuarantineResultDetailItem.ITEMS);
				for(int i = 0; i < quaItems.length(); i++) {
					JSONObject itemObject = quaItems.getJSONObject(i);
					QuarantineDetailListItem quaItem = new QuarantineDetailListItem();
					quaItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.OFFCD)));
					quaItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RECNO)));
					quaItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RECSEQ)));
					quaItem.setAftDsuseTrgetCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.AFTDSUSETRGETCD)));
					quaItem.setDbyhsProcessCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.DBYHSPROCESSCD)));
					quaItem.setPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PRDLSTCODE)));
					quaItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PRDLSTNM)));
					quaItem.setItmQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ITMQTY)));
					quaItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.INSQTY)));
					quaItem.setQunCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.QUNCD)));
					quaItem.setLmoAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.LMOAT)));
					quaItem.setCntanrLnlAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.CNTANRLNLAT)));
					quaItem.setIslctvtAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ISLCTVTAT)));
					quaItem.setQrantExclAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.QRANTEXCLAT)));
					quaItem.setPieximrAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PIEXIMRAT)));
					quaItem.setResultFlag(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RESULTFLAG)));
					quaItem.setItemType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ITEMTYPE)));
					quaItem.setHsCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.HSCD)));
					arrQuarantine.add(quaItem);
				}
				quarantineItem.setItems(arrQuarantine);
				quarantineItem.setItemsResult(json.getString(QuarantineResultDetailItem.ITEMSRESULT));
				
				quarantineItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RESOFFCD)));
				quarantineItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RESNO)));
				quarantineItem.setRegType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.REGTYPE)));
				quarantineItem.setMsgId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.MSGID)));
				quarantineItem.setTsqno(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.TSQNO)));
				quarantineItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.OFFCD)));
				quarantineItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RECNO)));
				quarantineItem.setNatCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.NATCD)));
				quarantineItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.NATNM)));
				quarantineItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.VENNM)));
				quarantineItem.setVenRep(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.VENREP)));
				quarantineItem.setVenTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.VENTELNO)));
				quarantineItem.setDelayNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.DELAYNM)));
				quarantineItem.setDelayRcognAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.DELAYRCOGNAT)));
				quarantineItem.setValidflag(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.VALIDFLAG)));
				quarantineItem.setResultProgrsSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RESULTPROGRSST)));
				quarantineItem.setResultProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RESULTPROGRSNM)));
				quarantineItem.setEndDivYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.ENDDIVYN)));
				quarantineItem.setResDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.RESDE)));
				quarantineItem.setFieDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.FIEDE)));
				quarantineItem.setInsPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.INSPLACE)));
				quarantineItem.setPzsCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.PZSCNT)));
				quarantineItem.setPenCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.PENCNT)));
				quarantineItem.setExpCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.EXPCNT)));
				quarantineItem.setDbyhsCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.DBYHSCNT)));
				quarantineItem.setDelaysayu(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.DELAYSAYU)));
				quarantineItem.setImgResult1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.IMGRESULT1)));
				quarantineItem.setImgResult2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.IMGRESULT2)));
				quarantineItem.setFileId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.FILEID)));
				quarantineItem.setQuranNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.QURANNM1)));
				quarantineItem.setQuranNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.QURANNM2)));
				quarantineItem.setQuranCd1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.QURANCD1)));
				quarantineItem.setQuranCd2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.QURANCD2)));
				quarantineItem.setBlNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.BLNO)));
				quarantineItem.setHwaNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.HWANO)));
				quarantineItem.setRemark(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.REMARK)));
				quarantineItem.setTrnNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.TRNNM)));
				quarantineItem.setTrnCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultDetailItem.TRNCD)));
				callback.OnQuarantineDetailCompleted(true, quarantineItem, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		
	}
}
